package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Student;
import com.itheima.service.UserService;
import com.itheima.utils.SqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {
    SqlSession sqlSession = SqlSessionFactory.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    @Override
    public Student loginUser(String username, String password) {
        Student student = userMapper.loginUser(username, password);
        sqlSession.close();
        return student;
    }

    @Override
    public int addUser(Student student) {
        int affNum = userMapper.addUser(student);
        System.out.println(student.getId());
        sqlSession.commit();
        return student.getId();
    }

    @Override
    public boolean delectUser(int id) {
        int affNum = userMapper.delectUser(id);
        sqlSession.commit();
        return affNum>0;
    }

    @Override
    public boolean update(Student student) {
        int affNum = userMapper.updateUser(student);
        sqlSession.commit();
        return affNum>0;
    }

    @Override
    public List<Student> findAll() {
        List<Student> students = userMapper.findAll();
        return students;
    }

    @Override
    public List<Student> findByName(String username) {
        List<Student> students = userMapper.findUserByName(username);
        return students;
    }

    @Override
    public Student findById(int id) {
        Student student = userMapper.findUserById(id);
        return student;
    }
}
